
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import '../event/user_id_event.dart';
import '../http/api.dart';
import '../main.dart';
import '../socket/socket_manager.dart';
import '../util/timer_manager.dart';
import '../widget/app_bar.dart';
import '../widget/custom_floating_action_button_location.dart';
import '../widget/float_button.dart';
import '../widget/id_bind_dialog.dart';


class Register extends StatefulWidget {
  const Register({Key? key}) : super(key: key);

  @override
  State<Register> createState() => _RegisterState();
}

class _RegisterState extends State<Register> {

  SocketManager manager = SocketManager();
  StreamSubscription? userSubscription;


  @override
  initState() {
    super.initState();

    userSubscription = eventFactory.on<UserIdEvent>().listen((event) async {
      var data = await findReader(event.uid,category: 'icr_uid');
      if(data == null){
        IdBindDialog().idBindDialog(event.uid);
        return;
      }
      if(!data.validReader!){
        IdBindDialog().idBindDialog(event.uid);
        return;
      }
      IdBindDialog().registerFailedDialog('身份证号已注册');
    });

    WidgetsBinding.instance.addPostFrameCallback((_){
      timer.startCountDown(CountDownMode.action,(){
        currentTask.clear();
        timer.cancel();
        manager.setModeAndStart(Mode.none);
        Get.until((route) => route.isFirst);
      });

    });

    tts.speak('请刷身份证');
    manager.setModeAndStart(Mode.identify);

  }

  @override
  void dispose() {
    userSubscription?.cancel();
    userSubscription = null;
    super.dispose();
  }


  _title(){
    return Container(
        margin: EdgeInsets.only(top: 10.h,left: 8.w,right: 8.w,bottom: 10.h),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            InkWell(
              onTap: (){
                currentTask.clear();
                manager.setModeAndStart(Mode.none);
                Get.back();
              },
              child: Image.asset("assets/images/back.png", width: 40.w, fit: BoxFit.fitWidth,),
            ),
            Text("办证",style: TextStyle(color: Colors.white,fontSize: 12.sp,fontWeight: FontWeight.bold),),
            Obx(() => Text("${timer.actionTime.value}",style: TextStyle(color: Colors.white,fontSize: 8.sp),))
          ],
        )
    );
  }

  _container(){
    return Container(
        decoration:BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.all(Radius.circular(4.r)),
        ),
        margin: EdgeInsets.only(left: 20.w,right: 20.w,top: 20.h),
        padding: EdgeInsets.only(left: 16.w,right: 16.w,top: 12.h,bottom: 6.h),
        width: double.infinity,
        height: 540.h,
        child: Stack(
          alignment: AlignmentDirectional.center,
          children: [
            Positioned(
              top: 40.h,
              child:  Text('放置身份证',style: TextStyle(color: Colors.blue,fontSize: 14.sp,fontWeight: FontWeight.bold),),
            ),
            Positioned(
              top: 65.h,
              child:  Text('请在读卡区域放置您的身份证',style: TextStyle(color: Colors.blue,fontSize: 10.sp,fontWeight: FontWeight.bold),),
            ),

            Positioned(
              top: 100.h,
              child: Image.asset("assets/images/mode.png",width: 200.w, fit: BoxFit.fitWidth,),
            ),
            Positioned(
              top: 420.h,
              right: 40.w,
              child: Image.asset("assets/images/card.png",width: 60.w, fit: BoxFit.fitWidth,),
            ),
          ],
        ),
    );
  }

  _step(){
    return Align(child: Image.asset("assets/images/auth_step3.png",height:46.h,fit: BoxFit.fitHeight,));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButton:floatButton(),
      floatingActionButtonLocation:CustomFloatingActionButtonLocation(
          FloatingActionButtonLocation.endFloat, 0,-60.h),
      body: Container(
        width: double.infinity,
        height: double.infinity,
        decoration: const BoxDecoration(
          image: DecorationImage(
            fit: BoxFit.fill,
            image: AssetImage(
              "assets/images/bg.png",
            ),
          ),
        ),
        child: Column(
          children: [
            appBar(),
            _title(),
            _step(),
            _container()
          ],
        ),
      ),
    );
  }
}

